<?php
/**
 * @category 	BoardDao.php
 * @author 		Sung Hoon, Cho (akdddnet@gmail.com)
 * @since		2011. 7. 04.
 * @license		BSD License
 * @version		0.1
 */

global $CFG;
require_once $CFG->FW_DIR."/mvc/Dao.php";
 
class BoardDao extends Dao {
	
 	function __construct(array $db_type = null) {
 		parent::__construct($db_type);	
 	}
 	
 	function index(DTO $dto) {
 		$query = "
 				SELECT 
 							*
 				FROM 		board_admin
 				WHERE		1 = 1
 				AND			ba_ddate IS NULL
 		";
 		
 		if(!Common::isNullOrEmpty($dto->paging)) {
 			$query .= " 
 				LIMIT ".$dto->paging->start.", ".$dto->paging->ls;
 		}

 		$dto->list = $this->db->query($query)->fetchAll();
 		
 		return $dto;
 	}
 	
 	function view(DTO $dto) {
 		$query ="
 				SELECT		*
 				FROM 		board_admin
 				WHERE		1 = 1
 				AND			ba_seq = :ba_seq
 				AND			ba_ddate IS NULL
 		";
 		
 		$bind = array(
 				"ba_seq"=>$dto->eb->ba_seq
 		);
 		
 		$dto->obj = $this->db->query($query, $bind)->fetch();
 		
 		return $dto;
 	}
 	
 	function doAdd(DTO $dto) {
 		
 		$rc = 0;
 		
 		$query = "
 				INSERT INTO board_admin
 					(ba_id, ba_name, ba_skin, ba_category, ba_footlist, ba_newicontime, ba_attach, ba_listingnum, ba_pagingnum, ba_thumwidth, ba_thumheight, ba_cdate)
 				VALUES
 					(:ba_id, :ba_name, :ba_skin, :ba_category, :ba_footlist, :ba_newicontime, :ba_attach, :ba_listingnum, :ba_pagingnum, :ba_thumwidth, :ba_thumheight, now())
 		";
 		
 		$bind = array(
 			 "ba_id"=>$dto->eb->ba_id
 			,"ba_name"=>$dto->eb->ba_name
 			,"ba_skin"=>$dto->eb->ba_skin
 			,"ba_category"=>$dto->eb->ba_category
 			,"ba_footlist"=>$dto->eb->ba_footlist
 			,"ba_newicontime"=>$dto->eb->ba_newicontime
 			,"ba_attach"=>$dto->eb->ba_attach
 			,"ba_listingnum"=>$dto->eb->ba_listingnum
 			,"ba_pagingnum"=>$dto->eb->ba_pagingnum
 			,"ba_thumwidth"=>$dto->eb->ba_thumwidth
 			,"ba_thumheight"=>$dto->eb->ba_thumheight
 		);
 		
 		if($this->db->query($query, $bind)->rowCount() > 0) {

 			// 카테고리 등록
 			$ba_seq = $this->db->insertId();
 			$query = "
 				INSERT INTO board_category
 					(ba_seq, bc_name, bc_cdate, bc_delete)
 				VALUES
 					(:ba_seq, :bc_name, now(), 0)
 			";
 			
 			$bind = array(
 				 "ba_seq"=>$ba_seq
 				,"bc_name"=>"일반"
 			);
 			
 			$rc = $this->db->query($query, $bind)->rowCount();
 			$dto->obj = $ba_seq;
 		}
 		
 		$dto->rc = $rc;
 		
 		return $dto;
 	}
 	
 	
	function getID(DTO $dto) {
 		$query = "
 				SELECT
 							COUNT(*)
 				FROM		board_admin
 				WHERE		1 = 1
 				AND			ba_id = :ba_id
 		";
 		
 		$bind = array(
 				"ba_id"=>$dto->eb->ba_id
 		);
 		
 		$dto->obj = $this->db->query($query, $bind)->field();
 		
 		return $dto;
 	}
 	
 	
	function doModify(DTO $dto) {
		
 		$query = "
 				UPDATE 	board_admin SET
	 					 ba_name 			= :ba_name
	 					,ba_skin			= :ba_skin
	 					,ba_category		= :ba_category
	 					,ba_footlist		= :ba_footlist
	 					,ba_newicontime 	= :ba_newicontime
	 					,ba_attach			= :ba_attach
	 					,ba_listingnum		= :ba_listingnum
	 					,ba_pagingnum		= :ba_pagingnum
	 					,ba_thumwidth		= :ba_thumwidth
	 					,ba_thumheight		= :ba_thumheight
 				WHERE	1 = 1
 				AND		ba_seq			= :ba_seq
 		";
 		
 		$bind = array(
 			 "ba_seq"=>$dto->eb->ba_seq
 			,"ba_name"=>$dto->eb->ba_name
 			,"ba_skin"=>$dto->eb->ba_skin
 			,"ba_category"=>$dto->eb->ba_category
 			,"ba_footlist"=>$dto->eb->ba_footlist
 			,"ba_newicontime"=>$dto->eb->ba_newicontime
 			,"ba_attach"=>$dto->eb->ba_attach
 			,"ba_listingnum"=>$dto->eb->ba_listingnum
 			,"ba_pagingnum"=>$dto->eb->ba_pagingnum
 			,"ba_thumwidth"=>$dto->eb->ba_thumwidth
 			,"ba_thumheight"=>$dto->eb->ba_thumheight
 		);
 		
 		//$dto->rc = $this->db->query($query, $bind)->rowCount();
 		$this->db->query($query, $bind);
 		 		 
 		
 		
 		// 메뉴 등록을 체크 한 경우
 		$menu = $board_admin_eb->ba_menu;
 		if($menu) {
 			if(Common::isNullOrEmpty($menuinfo)) {
 				
 				// 기존에 등록된 메뉴가 있는지 확인.
 				$icon = $dto->eb->ba_icon;
 				
 					
 				$menu_eb = new MenuEB();
 				$menu_eb->mn_name = $board_admin_eb->ba_name;
 				$menu_eb->mn_icon = $board_admin_eb->ba_icon;
 				$menu_eb->mn_url = "/board/".$board_admin_eb->ba_id;
 				$menu_eb->mn_type = "board";
 				$menu_eb->mn_num = $board_admin_eb->ba_seq;
 					
 				$dto->eb = $menu_eb;
 					
 				$menu_dao->doExternalAdd($dto);
 			}
 		} else {
 			if(!Common::isNullOrEmpty($menuinfo)) {
 				// 기존에 등록된 메뉴 삭제.
 				$menu_dao->doExternalDelete($dto);
 			}
 		}
 		
 		
 		return $dto;
 	}
 	
 	function doDelete(DTO $dto) {
 		$query = "
 			UPDATE 	board_admin SET
 					ba_ddate = now()
 			WHERE	1 =1
 			AND		ba_id	= :ba_id  			
 		";
 		
 		$bind = array(
 			"ba_id"=>$dto->eb->ba_id
 		);
 		
 		$dto->rc = $this->db->query($query, $bind)->rowCount();
 		
 		return $dto;
 	}
 	
 	
 	function getTotalCount(DTO $dto) {
 		$query = "
 			SELECT 
 						COUNT(*) 
 			FROM 		board_admin
 			WHERE		1 = 1
 		";
 		
 		$dto->rc = $this->db->query($query)->field();
 		
 		return $dto;
 	}
 	
 	
 	function category(DTO $dto) {
 		$query = "
 			SELECT 
 						bc.*, ba.ba_id
 			FROM		board_category 	bc
 			LEFT JOIN	board_admin		ba ON ba.ba_seq = bc.ba_seq
 			WHERE		1 = 1
 			AND			bc.ba_seq = :ba_seq
 			AND			bc_ddate IS NULL
 			ORDER BY	bc_order ASC
 		";
 		
 		$bind = array(
 				"ba_seq"=>$dto->eb->ba_seq
 		);
 		
 		$dto->list = $this->db->query($query, $bind)->fetchAll();
 		
 		return $dto;
 	}
 	
 	
 	function doCategoryOverlap(DTO $dto) {
 		$query = "
 					SELECT
 							COUNT(*)
 					FROM	board_category
 					WHERE	1 = 1
 					AND		bc_name = :bc_name
 					AND		ba_seq 	= :ba_seq
 		";
 		
 		$bind = array(
 						"bc_name"=>$dto->eb->bc_name
 						,"ba_seq"=>$dto->eb->ba_seq
 		);
 		
 		$dto->rc = $this->db->query($query, $bind)->field();
 		
 		return $dto;
 	}
 	
 	
	function doCategoryAdd(DTO $dto) {
 		
 		
 		$query = "
 				INSERT INTO board_category
 					(ba_seq, bc_name, bc_order, bc_cdate, bc_delete)
 				VALUES
 				(
 						 :ba_seq
 						,:bc_name
 						,(SELECT A.bc_order FROM (SELECT MAX(bc_order) AS bc_order FROM board_category WHERE 1 = 1 AND ba_seq = :ba_seq) A)
 						,now()
 						,1
 				)
 		";
 		
 		$bind = array(
 			 "ba_seq"=>$dto->eb->ba_seq
 			,"bc_name"=>$dto->eb->bc_name
 		);
 		
 		
 		$dto->rc = $this->db->query($query, $bind)->rowCount();
 		
 		return $dto;
 	}
 	
	
 	function doCategoryDelete(DTO $dto) {
 		
 		// ba_seq으로 게시판 아이디 알아오기
 		$query = "
 					SELECT 	ba_id
 					FROM	board_admin
 					WHERE	1 = 1
 					AND		ba_seq = :ba_seq
 		";
 		$bind = array("ba_seq"=>$dto->eb->ba_seq);
 		
 		$ba_id = $this->db->query($query, $bind)->field();
 		
 		
 		
 		// 일반 카테고리 정보가져오기
 		$query = "
 					SELECT 
 							bc_seq, bc_count
 					FROM 	board_category
 					WHERE 	1 = 1
 					AND		ba_seq = :ba_seq
 					AND		bc_delete = 0
 		";
 		
 		$bind = array(
 					"ba_seq"=>$dto->eb->ba_seq
 		);
 		
 		$category = $this->db->query($query, $bind)->fetch();
 		
 		
 		// 트렌젝션 시작
 		$this->db->begin();
 		
 		// 삭제되는 카테고리를 일반 카테고리로 update
 		$query = "
 					UPDATE 	board SET
 							bc_seq = :change_bc_seq
 					WHERE	1 = 1
 					AND		ba_id = :ba_id
 					AND		bc_seq = :bc_seq
 		";
 		
 		
 		$bind = array(
 				"ba_id"=>$ba_id
 				,"change_bc_seq"=>$category->bc_seq
 				,"bc_seq"=>$dto->eb->bc_seq
 		);
 		
 		$this->db->query($query, $bind);
 		
 		
 		// 기존에 등록되어 있던 게시물 수를 일반 게시물 수에 update시키기
 		$query = "
 				UPDATE board_category SET
 					bc_count = bc_count + :bc_count
 				WHERE	1 = 1
 				AND		bc_seq = :bc_seq
 		";
 		$bind = array(
 				"bc_count"=>$category->bc_count
 				,"bc_seq"=>$category->bc_seq
 		);
 		
 		$this->db->query($query, $bind);
 		
 		
 		
 		$query = "
 				DELETE FROM board_category
 				WHERE 1 = 1
 				AND		ba_seq = :ba_seq
 				AND		bc_seq = :bc_seq
 		";
 		
 		$bind = array(
 			 "ba_seq"=>$dto->eb->ba_seq
 			,"bc_seq"=>$dto->eb->bc_seq
 		);
 		
 		
 		if($dto->rc = $this->db->query($query, $bind)->rowCount()) {
 			$this->db->commit();	
 		} else {
 			$this->db->rollback();
 		}
 		
 		
 		return $dto;
 	}
 	
 	
 	
	function categoryView(DTO $dto) {
 		$query ="
 				SELECT		*
 				FROM 		board_category
 				WHERE		1 = 1
 				AND			ba_seq = :ba_seq
 				AND			bc_seq = :bc_seq
 				AND			bc_ddate IS NULL
 		";
 		
 		$bind = array(
 				 "ba_seq"=>$dto->eb->ba_seq
 				,"bc_seq"=>$dto->eb->bc_seq
 		);
 		
 		$dto->obj = $this->db->query($query, $bind)->fetch();
 		
 		return $dto;
 	}
 	
 	
 	
	function doCategoryModify(DTO $dto) {
 		$query ="
 				UPDATE	board_category SET
 						bc_name = :bc_name
 				WHERE	1 = 1
 				AND		bc_seq = :bc_seq
 				AND		ba_seq = :ba_seq
 		";
 		
 		$bind = array(
 				 "ba_seq"=>$dto->eb->ba_seq
 				,"bc_seq"=>$dto->eb->bc_seq
 				,"bc_name"=>$dto->eb->bc_name
 		);
 		
 		//$dto->rc = $this->db->query($query, $bind);
 		$this->db->query($query, $bind);
 		
 		return $dto;
 	}
}